package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    @Resource
    private ServeMapper serveMapper;
    @Resource
    private ServeItemMapper serveItemMapper;
    @Resource
    private RegionMapper regionMapper;


    @Override
    public PageResult<ServeResDTO> pageByDto(ServePageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto, () -> baseMapper.pageByRegion(dto.getRegionId()));

    }

    @Override
    public void updatePrice(Long id, Double price) {
        //根据区域id更改服务价格
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if (!update) {
            throw new RuntimeException(id + "区域修改价格失败");
        }
    }

    @Override
    public void batchServe(List<ServeResDTO> dtos) {
        //使用for循环遍历集合
        for (ServeResDTO dto : dtos) {
            //判断项目有没有被启用
            ServeItem serveItem = serveItemMapper.selectById(dto.getServeItemId());
            if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus() || serveItem == null) {
                throw new RuntimeException("不能添加未启用的服务:" + serveItem.getId());
            }
            //判断该区域是否存在该项目
            Integer count = lambdaQuery().eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();
            if (count > 0) {
                throw new RuntimeException("已存在该项目");
            }
            //将传进来的参数转为serve对象
            Serve serve = BeanUtil.toBean(dto, Serve.class);
            Region region = regionMapper.selectById(dto.getRegionId());
            serve.setCityCode(region.getCityCode());
            save(serve);
        }
    }

    @Override
    public void updateByItemId(Long itemId) {
        Serve serve = serveMapper.selectById(itemId);
        //如果项目的状态已经是上架的就抛出
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus() || serve == null) {
            throw new RuntimeException("状态异常");
        }
        //更改状态
        serve.setSaleStatus(FoundationStatusEnum.ENABLE.getStatus());
        updateById(serve);
    }

    @Override
    public void updateByServeDo(Long serveId) {
        //如果项目状态不是上架的就抛出
        Serve serve = serveMapper.selectById(serveId);
        if (serve == null || serve.getSaleStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new RuntimeException("状态异常");
        }
        //更改状态
        serve.setSaleStatus(FoundationStatusEnum.INIT.getStatus());
        updateById(serve);
    }

    @Override
    public void deleteByServeId(Long serveId) {
        //判断项目状态，如果状态为上架则不允许删除
        Serve serve = serveMapper.selectById(serveId);
        if (serve == null || serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus() || serve.getSaleStatus() == FoundationStatusEnum.DISABLE.getStatus()) {
            throw new RuntimeException("请先更改服务项目状态再尝试删除！");
        }
        //判断服务项目是否存在
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (serveItem == null) {
            throw new RuntimeException("服务项目不存在！");
        }
        serveMapper.deleteById(serveId);
    }

    @Override
    public void updateServeHotUp(Long serveId) {
        //判断服务项目的热门状态，如果已经设置为热门了则抛出异常
        Serve serve = serveMapper.selectById(serveId);
        if (serve == null || serve.getIsHot() == 1) {
            throw new RuntimeException("已经为热门状态，请勿重新添加！");
        }
        //设置服务状态为热门
        serve.setIsHot(1);
        updateById(serve);
    }

    @Override
    public void updateServeHotDo(Long serveId) {
        //判断服务项目的热门状态，如果已经不是热门状态了则抛出异常
        Serve serve = serveMapper.selectById(serveId);
        if (serve == null || serve.getIsHot() == 0) {
            throw new RuntimeException("状态有误");
        }
        //设置服务状态为热门
        serve.setIsHot(0);
        updateById(serve);
    }
}
